1. Field of the Invention
The present invention generally relates to a method for handling network packets, and more particularly, to a method for handling multiple network packets in an environment using a simplified TCP/IP stack.
2. Description of Related Art
The existing TCP/IP stacks include a complete TCP/IP stack and a simplified TCP/IP stack. In the complete TCP/IP stack, the Ethernet driver layer and the TCP/IP stack layer respectively maintain a network packet queue, and the Ethernet driver layer and the TCP/IP stack layer only process the network packets in the network packet queues correspondingly when they obtain the system execution right. By using the complete TCP/IP stack, the division of different layers is made very clear; however, relatively more memory space and processor resources are consumed. Besides, the two network packet queues have to be maintained by an operating system, and the support of a multi-threaded environment is required.
However, regarding a non-OS embedded system, a simplified TCP/IP stack has to be used in order to overcome the hardware restrictions of the system in memory space and processor speed. In a simplified TCP/IP stack layer, the network packets are stored in a buffer instead of a queue. To be specific, when a network packet is received by the Ethernet driver layer, the network packet is copied into the buffer right away and then handled by the TCP/IP stack layer. In short, each time only one network packet can be received and handled by the simplified TCP/IP stack.
In practice, an embedded system is connected to a network through a network device, such as a network card. If the network card has a peripheral controller interface (PCI) which can generate an interrupt signal when the network card receives a network packet each time, it issues an interrupt signal to the system to ensure that the network packet can be correctly copied from the Ethernet driver layer to the TCP/IP stack layer. However, if the network card has a universal serial bus (USB) interface or a secure digital input/output (SDIO) interface, the system itself has to request data from the network card because these two interfaces cannot generate any interrupt signal. Accordingly, the number of network packets transmitted by the network card to the system cannot be controlled.
To resolve the foregoing problem, the network packets can be stored all together into a buffer first and then analyzed individually. The frame formats of most network packets contain the information of packet length. Even if the frame format of a network packet does not contain the packet length information, the network packet has a fixed packet length as long as it conforms to the specification of a specific network protocol. Thus, a network packet can be correctly handled as long as the packet length thereof is obtained from a frame header before the network packets stored in the buffer are analyzed. However, not every network packet conforms to the specification of a network protocol. As to a network packet which does not conform to the specification of any network protocol and has no packet length information in the frame header thereof, the packet length of the network packet cannot be obtained correctly, and accordingly, the start address of the network packet in the buffer may be wrongly determined when the TCP/IP stack layer obtains the network packet from the buffer. As a result, packet loss or system abnormity may be caused.